package com.sanpu.kxe.module.dialog;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.module.roundview.RoundTextView;

import butterknife.ButterKnife;

/**
 * Description：升级推送消息弹框
 * Created by 薛勇军 on 2019/3/22.
 */

public class PushMsgUpgradeDialog {

    private CharSequence content;
    private TextView vContent;
    private TextView vTitle;
    private RoundTextView vClose;
    private Context context;
    private String title;

    public PushMsgUpgradeDialog content(String content) {
        this.content = content;
        return this;
    }

    public PushMsgUpgradeDialog title(String title) {
        this.title = title;
        return this;
    }

    public PushMsgUpgradeDialog timestamp(String timestamp) {
        return this;
    }

    public PushMsgUpgradeDialog(Context context) {
        this.context = context;
    }

    public void show() {
        final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        final View view = View.inflate(context, R.layout.dialog_push_msg, null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                windowManager.removeView(view);
            }
        });

        try {
            ButterKnife.findById(view, R.id.close).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    windowManager.removeView(view);
                }
            });

            ButterKnife.findById(view, R.id.background).setAnimation(AnimationUtils.loadAnimation(context, R.anim.alpha_in));

            vContent = ButterKnife.findById(view, R.id.content);
            vTitle = ButterKnife.findById(view, R.id.title);
            vClose = ButterKnife.findById(view, R.id.close);

            if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(content)) {
                vContent.setText(content);
                vTitle.setText(title);
            }

            WindowManager.LayoutParams params = new WindowManager.LayoutParams();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //26及以上必须使用TYPE_APPLICATION_OVERLAY   @deprecated TYPE_PHONE
                params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                params.type = WindowManager.LayoutParams.TYPE_PHONE;
            }
            params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            params.format = PixelFormat.TRANSLUCENT;
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.MATCH_PARENT;
            params.gravity = Gravity.TOP | Gravity.LEFT;
            windowManager.addView(view, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
